package dungeon;

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

import square.Direction;
import square.NormalSquare;
import square.Square;
import square.Rock;
import square.TeleportationSquare;
import square.Temperature;
import condition.ConditionDefault;
import condition.ConditionIsRock;

public class LeafDungeonTest {

	@Before
	public void setUp() throws Exception {
		level = new Level<Square>(5, 5, 0);
		shaft = new Shaft<Square>(0,5,0);
		square1 = new NormalSquare();
		square2 = new NormalSquare(new Temperature(100),new BigDecimal(0));
		level.addSquare(new Position(5,5,0), square1);
		level.addSquare(new Position(0,5,0), square2);
	}
	
	

	@Test
	public void testGetSquares() {
		Iterator<Square> it = level.getSquares(new ConditionDefault());
		Square s1 = it.next();
		Square s2 = it.next();
		assertTrue((s1 == square1 || s1 == square2));
		assertTrue((s1 == square2 || s2 == square2));
	}
	
	@Test
	public void terminate_singlecase(){
		shaft.addSquare(new Position(0,1,0), square1);
		shaft.addSquare(new Position(0,2,0), square2);
		assertTrue(level.hasAsSquare(square2));
		assertTrue(shaft.hasAsSquare(square2));
		shaft.terminate();
		level.terminate();
		assertEquals(0,level.getNbSquares());
		assertEquals(0, shaft.getNbSquares());
	}
	
	@Test
	public void addSquare_test(){
		Position p1 = new Position(0,1,0);
		level.addSquare(p1, new Rock());
		level.addSquare(new Position(0,2,0), new Rock());
		assertEquals(level.getSquareAt(new Position(0,1,0)).getBorder(Direction.SOUTH),level.getSquareAt(new Position(0,2,0)).getBorder(Direction.NORTH));
		level.addSquare(new Position(1,1,0), new NormalSquare());
		level.addSquare(new Position(1,2,0), new NormalSquare(new Temperature(100),new BigDecimal(0)));
		assertEquals(level.getSquareAt(new Position(1,1,0)).getTemperature(), level.getSquareAt(new Position(1,2,0)).getTemperature());
		assertEquals(level.getSquareAt(new Position(1,1,0)).getTemperature().getTemperatureValue(), level.getSquareAt(new Position(0,1,0)).getTemperature().getTemperatureValue(),0.5);
		HashSet<TeleportationSquare> r = level.getSpace(level.getSquareAt(new Position(1,1,0)));
		assertTrue(r.contains(level.getSquareAt(new Position(1,1,0))));
		assertTrue(r.contains(level.getSquareAt(new Position(1,2,0))));
		assertEquals(2,r.size());
	}
	
	@Test
	public void getAllSquares_test(){
		level.addSquare(new Position(0,1,0), new Rock());
		level.addSquare(new Position(0,2,0), new Rock());
		Iterator<Square> it = level.getSquares(new ConditionIsRock());
		it.next();
		assertTrue(it.hasNext());
		assertTrue(it.next() instanceof Rock);
		assertFalse(it.hasNext());
	}
	
	Level<Square> level;
	Shaft<Square> shaft;
	TeleportationSquare square1;
	TeleportationSquare square2;
}
